package weatehrcrowd.servlets;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import weathercrowd.managers.PersistenceManager;
import weathercrowd.model.*;
import weathercrowd.utilities.PersistenceHelper;
import weathercrowd.utilities.SessionHelper;

/**
 * Servlet implementation class UserInputServlet
 */
@WebServlet("/UserInputServlet")
public class UserInputServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UserInputServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		doGetOrPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGetOrPost(request, response);
	}
	
	protected void doGetOrPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String email = request.getParameter("emailid");
		String address = request.getParameter("locationres");
		String sLongitude = request.getParameter("longloc");
		String sLatitutde = request.getParameter("latloc");
		String sMin = request.getParameter("mintemp");
		String sMax = request.getParameter("maxtemp");
		String sRf = request.getParameter("rainfall");
		String sDate = request.getParameter("timepicker");
		
		
		String sHail = request.getParameter("hail");
		String sBushFires = request.getParameter("bushfires");
		String sHeavyStorm = request.getParameter("heavystorm");
		String sCyclone = request.getParameter("cyclone");
		String sFog = request.getParameter("fog");
		String sDustStorm = request.getParameter("duststorm");
		
		String sEventType = "";
		sEventType += sHail != null ? "1" : "0";
		sEventType += sBushFires != null ? "1" : "0";
		sEventType += sHeavyStorm != null ? "1" : "0";
		sEventType += sCyclone != null ? "1" : "0";
		sEventType += sFog != null ? "1" : "0";
		sEventType += sDustStorm != null ? "1" : "0";
		
		String comments = request.getParameter("description");
		
		HttpSession session = request.getSession();
		PersistenceManager pm = SessionHelper.getPersistenceManager(session);
		EntityManager em = SessionHelper.getEntityManager(session);
		EntityTransaction t = em.getTransaction();
		PersistenceHelper.beginTransaction(t);
		
		User user = pm.getOrCreateUser(email);
		UserLocation uloc = new UserLocation();
		UserRainfall rf = new UserRainfall();
		UserTemperature tmpr = new UserTemperature();
		Event evt = new Event();
		
		long locId = pm.generateLocationId();
		long rfId = pm.generateRainfallId();
		long tmprId = pm.generateTemperatureId();
		long evtId = pm.generateEventId();

		SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy hh:mm");
		Date date = null;
		try {
			date = formatter.parse(sDate);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		uloc.setId(locId);
		uloc.setStreet(address);
		uloc.setLongitude(Float.parseFloat(sLongitude));
		uloc.setLatitude(Float.parseFloat(sLatitutde));
		uloc.setName("User Input");
		
		rf.setId(rfId);
		rf.setUser(user);
		rf.setLocation(uloc);
		rf.setAmount(Float.parseFloat(sRf));
		rf.setDate(date);
		
		tmpr.setId(tmprId);
		tmpr.setUser(user);
		tmpr.setLocation(uloc);
		tmpr.setMin(Float.parseFloat(sMin));
		tmpr.setMax(Float.parseFloat(sMax));
		tmpr.setDate(date);
		
		evt.setComments(comments);
		evt.setEventType(sEventType);
		evt.setDate(date);
		evt.setId(evtId);
		evt.setLocation(uloc);
		
		em.persist(evt);
		em.persist(user);
		em.persist(uloc);
		em.persist(tmpr);
		em.persist(rf);
		
		t.commit();
		
		response.sendRedirect("./charts.html");
		
	}

}
